﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class BanDAO
    {
        // Operation...
        public static bool ThemBan(BanDTO ban)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Insert into BAN(TenBan, KhuVuc, TinhTrang) Values(@TenBan, @KhuVuc, @TinhTrang)";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@TenBan", OleDbType.VarChar);
                thamso.Value = ban.TenBan;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@KhuVuc", OleDbType.Integer);
                thamso.Value = ban.KhuVuc.MaKhuVuc;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@TinhTrang", OleDbType.VarChar);
                thamso.Value = ban.TinhTrang;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool XoaBan(KhuVucDTO khuvuc)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Delete From BAN Where KhuVuc = @KhuVuc";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso = new OleDbParameter("@KhuVuc", OleDbType.Integer);
                thamso.Value = khuvuc.MaKhuVuc;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua;
        }

        public static bool CapNhatTinhTrang(BanDTO ban, string tinhtrang)
        {
            bool ketqua = true;

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Update BAN set TinhTrang = @TinhTrang Where MaBan = @MaBan";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@TinhTrang", OleDbType.VarChar);
                thamso.Value = tinhtrang;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@MaBan", OleDbType.Integer);
                thamso.Value = ban.MaBan;
                lenh.Parameters.Add(thamso);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketqua = false;
            }

            return ketqua; 
        }

        public static bool TachBan(BanDTO banIn, BanDTO banOut)
        {
            return true;
        }

        public static bool GhepBan(BanDTO banIn, BanDTO banOut)
        {
            return true;
        }

        public static List<BanDTO> LayDanhSachBan(long makhuvuc)
        {
            List<BanDTO> dsBan = new List<BanDTO>();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select TenBan, TinhTrang From BAN Where KhuVuc = @KhuVuc";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso = new OleDbParameter("@KhuVuc", OleDbType.Integer);
                thamso.Value = makhuvuc;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    BanDTO ban = new BanDTO();
                    ban.TenBan = bodoc.GetString(0);

                    if (!bodoc.IsDBNull(1))
                        ban.TinhTrang = bodoc.GetString(1);

                    dsBan.Add(ban);
                }
            }
            catch (Exception ex)
            {
                dsBan = new List<BanDTO>();
            }

            return dsBan;
        }

        public static BanDTO LayThongTinBan(string tenban, KhuVucDTO khuvuc)
        {
            BanDTO ban = new BanDTO();

            try
            {
                OleDbConnection ketnoi = ConnectionDAO.ketnoi;
                string chuoilenh = "Select MaBan, TinhTrang From BAN Where TenBan = @TenBan and KhuVuc = @KhuVuc";
                OleDbCommand lenh = new OleDbCommand(chuoilenh, ketnoi);
                OleDbParameter thamso;

                thamso = new OleDbParameter("@TenBan", OleDbType.VarChar);
                thamso.Value = tenban;
                lenh.Parameters.Add(thamso);

                thamso = new OleDbParameter("@KhuVuc", OleDbType.Integer);
                thamso.Value = khuvuc.MaKhuVuc;
                lenh.Parameters.Add(thamso);

                OleDbDataReader bodoc = lenh.ExecuteReader();
                while (bodoc.Read())
                {
                    ban.MaBan = bodoc.GetInt32(0);
                    if (!bodoc.IsDBNull(1))
                        ban.TinhTrang = bodoc.GetString(1);
                }
            }
            catch (Exception ex)
            {
                ban = new BanDTO();
            }

            return ban;
        }
    }
}
